import React, { Component } from 'react';

import './home.scss';
import Header from '../../component/Header'
import TabBottom from '../../component/TabBottom'
import MenuList from './MenuList'
import Banner from './Banner'
import  {SetUser,SetUserAsync}from '../../redux'
import {connect} from 'react-redux'

import axios from '../../util/axios'
import {api}  from '../../api'
import * as dd from 'dingtalk-jsapi'; 
class Home extends Component {
  constructor(props){
    super(props)
    this.state={
      selectActive:"Home"
    }
   this._selectAction=this._selectAction.bind(this)
  }
  getQueryVariable=(variable)=>{

       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return (false);

  }
  componentDidMount(){
    let self=this;
 
   
    //let defaultUserData={id:'2782',userName:'测试用车',userPwd:'202cb962ac59075b964b07152d234b70',userType:3,isType:'0',userDeptId:2645,userPhone:'18069781982',driverType:null}
  let defaultUserData= {id:'',userName:'',userPwd:'',userType:'',isType:'',userDeptId:'',userPhone:'',driverType:null}
     sessionStorage.user=JSON.stringify(defaultUserData);
   //   sessionStorage.user=JSON.stringify(userData);

//  if(dd.env.platform=="notInDingTalk"){
//     console.log(dd.env.platform=="notInDingTalk")
//    return;
 
//  }
//let  ID = "ding5d2dd335032d4436"; //企业ID 政务网
//let ID = "ding18f00fd4d9b0386535c2f4657eb6378f"; //企业ID wenzhou
 //let  ID = "dingc46dcea95bcb50da35c2f4657eb6378f"; //企业ID 嘉兴

//  dd.ready(function () {
//    dd.runtime.permission.requestAuthCode({
//      corpId: ID,
//      onSuccess: function (result) {
//        console.log("-------->result", result.code);
//        axios.get(api.h5Dd + 'code=' + result.code)
//          .then(respone => {

//            if (respone.result && respone.result == "ok") {
//              sessionStorage.user = JSON.stringify(respone.data[0]);
//              // self.props.SetUserAsync(
//              //    respone.data[0]
//              //  );
//            } else {
//              alert("没有该用户")
//            }

//          }).then(err => {
//            console.log(err)
//          })

//      },
//      onFail: function (err) {
//        console.log("err", err);
//      }

//    });

//  });
   let phone=this.getQueryVariable("phone");
      if(!phone){
        return  alert("没有该用户!")
      }
       axios.get(api.phoneLogin + '&userPhone=' + phone)
         .then(respone => {

           if (respone.result && respone.result == "ok") {
             sessionStorage.user = JSON.stringify(respone.data);
             // self.props.SetUserAsync(
             //    respone.data[0]
             //  );
           } else {
             if(respone.msg!=""){
                alert(respone.msg)
               }else{
                 alert("没有该用户")
             }
           }

         }).then(err => {
           console.log(err)
         })
    
  }
  _selectAction({name}){
   this.setState({
      selectActive:name
    })
  }
  render() {
    return (
      <div className="Home">
           <Header/>
           <div className="body">
              <Banner/>
              <MenuList 
              selectActive={this.state.selectActive}
              history={this.props.history}
              />
           </div>
           {/* <TabBottom 
             selectAction={this._selectAction}
           /> */}
       </div>
    );
  }
}
const  mapStateToProps = (state, ownProps) => {
    return {
      user: state.user
   }
  }
  const actionCreators= {SetUser,SetUserAsync} ;
  Home=connect(mapStateToProps,actionCreators)(Home)
  export default  Home
